package com.guigu.dh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.dh.enums.AuthStatusEnum;
import com.guigu.dh.mapper.UserInfoMapper;
import com.guigu.dh.pojo.user.UserInfo;
import com.guigu.dh.service.UserInfoService;
import com.guigu.dh.sms.feign.SmsFeign;
import com.guigu.dh.vo.user.UserAuthVo;
import com.guigu.dh.vo.user.UserInfoQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 用户服务层
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private SmsFeign smsFeign;

    // 根据号码查询用户信息
    @Override
    public UserInfo findUserInfoByPhone(String phone) {
        UserInfo userInfo = this.baseMapper.selectOne(
                new QueryWrapper<UserInfo>().eq("phone", phone));
        if (userInfo == null){
            return new UserInfo();
        } else {
            Integer authStatus = userInfo.getAuthStatus();
            String statusName = AuthStatusEnum.getStatusNameByStatus(authStatus);
            Map<String, Object> param = userInfo.getParam();
            param.put("statusName",statusName);
        }
        return userInfo;
    }

    // 根据手机号查询是否存在验证码
    @Override
    public Boolean getPhoneInfo(String phone) {
        return smsFeign.hasPhone(phone);
    }

    // 用户认证方法
    @Override
    public Boolean userAuth(String phone, UserAuthVo userAuthVo) {
        UserInfo userInfo = this.findUserInfoByPhone(phone);
        if (userInfo != null){
            // 存在则更新认证信息
            userInfo.setName(userAuthVo.getName());
            userInfo.setCertificatesType(userAuthVo.getCertificatesType());
            userInfo.setCertificatesNo(userAuthVo.getCertificatesNo());
            userInfo.setCertificatesUrl(userAuthVo.getCertificatesUrl());
            userInfo.setAuthStatus(AuthStatusEnum.AUTH_RUN.getStatus());
            int i = this.baseMapper.updateById(userInfo);
            return i>0;
        }
        return false;
    }

    // 查询用户列表并分页
    @Override
    public IPage<UserInfo> findAllPage(IPage<UserInfo> iPage, UserInfoQueryVo userInfoQueryVo) {
        // 取出条件
        String keyword = userInfoQueryVo.getKeyword();// 用户名称
        Integer authStatus = userInfoQueryVo.getAuthStatus();// 认证状态
        Integer status = userInfoQueryVo.getStatus();// 用户状态
        String createTimeBegin = userInfoQueryVo.getCreateTimeBegin();// 创建开始时间
        String createTimeEnd = userInfoQueryVo.getCreateTimeEnd();// 创建结束时间

        // 构建查询条件
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        // 判断条件是否为空
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like("name",keyword);
        }
        if (!StringUtils.isEmpty(authStatus)) {
            wrapper.eq("auth_status",authStatus);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge("create_time",createTimeBegin);
        }
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.le("create_time",createTimeEnd);
        }

        // 查询
        IPage<UserInfo> page = this.baseMapper.selectPage(iPage, wrapper);
        // System.out.println(page);
        return page;
    }

    // 修改锁定状态的方法
    @Override
    public Boolean lockStatus(Long id, Integer status) {
        UserInfo userInfo = new UserInfo();
        userInfo.setStatus(status);
        userInfo.setId(id);
        int i = this.baseMapper.updateById(userInfo);
        return i>0;
    }

}
